﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using LinqToLfsWorld.Extensions;

namespace LinqToLfsWorld.Queries
{
	/// <summary>
	/// Queries LfsWorld for racer statistics information.
	/// </summary>
	public class RacerStatsQuery : LfsWorldQuery<RacerStats>
	{

		/// <summary>
		/// Initializes a new instance of the <see cref="RacerStatsQuery"/> class.
		/// </summary>
		/// <param name="context">The context.</param>
		public RacerStatsQuery(LfsWorldContext context)
			: base(context)
		{
		}

		/// <summary>
		/// Gets the URL used to query LfsWorld.
		/// </summary>
		/// <returns></returns>
		protected override string GetUrl()
		{

			string url = LfsHelper.GetPubstatUrlBase(PubstatResponseType.Xml, this, "pst");

			url += "&racer=" + (string)ExpressionValues["RacerName"].Value;

			return url;
		}

		/// <summary>
		/// Gets the entities given LfsWorld response XML
		/// </summary>
		/// <param name="responseData">The response data.</param>
		/// <returns>A collection of Host entities</returns>
		protected override IEnumerable<RacerStats> ProcessResponse(string responseData)
		{
			XDocument doc = XDocument.Parse(responseData);
			XElement node = doc.Descendants("entry").SingleOrDefault();

			if (node != null)
			{

				RacerStats stats = new RacerStats();

				stats.RacerName = ((string)ExpressionValues["RacerName"].Value).StripLfsColours();
				stats.Distance = Convert.ToInt32(node.Element("distance").Value);
				stats.Fuel = Convert.ToInt32(node.Element("fuel").Value);
				stats.Laps = Convert.ToInt32(node.Element("laps").Value);
				stats.HostsJoined = Convert.ToInt32(node.Element("joined").Value);
				stats.Wins = Convert.ToInt32(node.Element("win").Value);
				stats.Seconds = Convert.ToInt32(node.Element("second").Value);
				stats.Thirds = Convert.ToInt32(node.Element("third").Value);
				stats.RacesFinished = Convert.ToInt32(node.Element("races_finished").Value);
				stats.Qualifications = Convert.ToInt32(node.Element("qual").Value);
				stats.PolePositions = Convert.ToInt32(node.Element("pole").Value);
				stats.DragRaces = Convert.ToInt32(node.Element("drags").Value);
				stats.DragRaceWins = Convert.ToInt32(node.Element("dragwins").Value);
				stats.Country = node.Element("country").Value;
				stats.OnlineStatus = (OnlineStatus)Convert.ToByte(node.Element("ostatus").Value);
				stats.LastHost = node.Element("hostname").Value.StripLfsColours();

				if (node.Element("last_time") != null)
				{
					int timestamp = Convert.ToInt32(node.Element("last_time").Value);
					stats.LastOnlineTime = DateTimeExtensions.FromUnixTimestamp(timestamp);
				}

				if (node.Element("track") != null)
				{
					stats.LastTrackIdent = node.Element("track").Value;
					stats.LastTrackName = Utility.CircuitNameFactory.GetNameFromIdent(stats.LastTrackIdent);
				}

				if (node.Element("car") != null)
					stats.LastCar = node.Element("car").Value;

				yield return stats;
			}
		}
	}
}
